###############################################
# === xenomorph_captive_development_effects.txt ===
###############################################
###############################################
# XENOMORPH CAPTIVE DEVELOPMENT - scripted effects (Layer 1)
# Initialization, action application, regen, and reward scaling.
###############################################

# ---------------------------------------------------------------------------
# INITIALIZATION - call in LEADER scope when a captive is first taken.
# ---------------------------------------------------------------------------
xeno_init_captive_state = {
	if = {
		limit = { NOT = { has_variable = xeno_submission } }
		set_variable = { which = xeno_submission value = 0 }
	}
	if = {
		limit = { NOT = { has_variable = xeno_vitality } }
		set_variable = { which = xeno_vitality value = 100 }
	}
}

# ---------------------------------------------------------------------------
# APPLY AN ACTION (leader scope). Spends vitality, raises submission, clamps.
#   $COST$   = vitality spent
#   $SUB$    = submission gained
# ---------------------------------------------------------------------------
xeno_captive_apply_action = {
	change_variable = { which = xeno_vitality value = -$COST$ }
	change_variable = { which = xeno_submission value = $SUB$ }
	# clamp vitality to [0,100]
	if = { limit = { check_variable = { which = xeno_vitality value < 0 } }   set_variable = { which = xeno_vitality value = 0 } }
	if = { limit = { check_variable = { which = xeno_vitality value > 100 } } set_variable = { which = xeno_vitality value = 100 } }
	# clamp submission to [0,100]
	if = { limit = { check_variable = { which = xeno_submission value < 0 } }   set_variable = { which = xeno_submission value = 0 } }
	if = { limit = { check_variable = { which = xeno_submission value > 100 } } set_variable = { which = xeno_submission value = 100 } }
}

# ---------------------------------------------------------------------------
# MONTHLY VITALITY REGEN (country scope; iterates captives).
# Captives regenerate +2/month UNTIL Broken (submission >= 70), then frozen.
# ---------------------------------------------------------------------------
xeno_captive_monthly_regen = {
	every_owned_leader = {
		limit = {
			has_leader_flag = xeno_captured_leader
			has_variable = xeno_vitality
			NOT = { xeno_captive_is_broken_or_beyond = yes }
			check_variable = { which = xeno_vitality value < 100 }
		}
		change_variable = { which = xeno_vitality value = 2 }
		if = {
			limit = { check_variable = { which = xeno_vitality value > 100 } }
			set_variable = { which = xeno_vitality value = 100 }
		}
	}
}

# ---------------------------------------------------------------------------
# REWARD SCALING (country scope). Adds the matching boost modifier, with
# duration scaled by the browsed captive's submission stage:
#   Defiant x1.0, Yielding x1.5, Submissive x2.0, Broken x2.5, Devoted x3.0
# (Layer 1: scales DURATION of the existing flat modifiers. Layer 2 can add
#  graduated modifier variants for magnitude scaling.)
#   $MOD$ = static modifier name to apply
# ---------------------------------------------------------------------------
xeno_captive_grant_scaled_reward = {
	if = {
		limit = { event_target:xeno_browsed_captive = { xeno_captive_is_devoted = yes } }
		add_modifier = { modifier = $MOD$ days = 10800 }
	}
	else_if = {
		limit = { event_target:xeno_browsed_captive = { xeno_captive_is_broken = yes } }
		add_modifier = { modifier = $MOD$ days = 9000 }
	}
	else_if = {
		limit = { event_target:xeno_browsed_captive = { xeno_captive_is_submissive = yes } }
		add_modifier = { modifier = $MOD$ days = 7200 }
	}
	else_if = {
		limit = { event_target:xeno_browsed_captive = { xeno_captive_is_yielding = yes } }
		add_modifier = { modifier = $MOD$ days = 5400 }
	}
	else = {
		add_modifier = { modifier = $MOD$ days = 3600 }
	}
}

###############################################
# === xenomorph_captive_layer2_effects.txt ===
###############################################
###############################################
# XENOMORPH CAPTIVE DEVELOPMENT - Layer 2 mechanics
#
# The effects that make the menu DO things:
#   - Lethal impregnation -> spawn a leader pop with copied class/traits + scaled XP
#   - Espionage conversion -> intel + spy network vs the captive's origin empire
#   - Release -> return captive to origin empire
#   - Roster capacity -> base 2 (+2 tradition, +2 tech, +2 AP)
#   - Death cleanup
#
# Most effects run in COUNTRY scope and operate on event_target:xeno_browsed_captive
# (the captive leader selected in the browse menu).
###############################################


# ---------------------------------------------------------------------------
# SPAWN A LEADER FROM A KILLED CAPTIVE (country scope).
# Reads the captive's class, species, and submission; creates a new leader of
# the same class & species; grants starting XP scaled by submission stage; and
# copies any Xenomorph leader traits the captive carried. Then kills the captive.
# ---------------------------------------------------------------------------
xeno_captive_spawn_leader_from_kill = {
	# Capture the captive's class + species into scopes before we create.
	event_target:xeno_browsed_captive = { save_event_target_as = xeno_dying_captive }

	# Determine submission-scaled starting experience.
	# Defiant 0, Yielding 200, Submissive 500, Broken 900, Devoted 1400.
	set_variable = { which = xeno_spawn_xp value = 0 }
	if = { limit = { event_target:xeno_dying_captive = { xeno_captive_is_yielding = yes } }   set_variable = { which = xeno_spawn_xp value = 200 } }
	if = { limit = { event_target:xeno_dying_captive = { xeno_captive_is_submissive = yes } } set_variable = { which = xeno_spawn_xp value = 500 } }
	if = { limit = { event_target:xeno_dying_captive = { xeno_captive_is_broken = yes } }     set_variable = { which = xeno_spawn_xp value = 900 } }
	if = { limit = { event_target:xeno_dying_captive = { xeno_captive_is_devoted = yes } }    set_variable = { which = xeno_spawn_xp value = 1400 } }

	# Create the new leader of the SAME CLASS as the host.
	if = {
		limit = { event_target:xeno_dying_captive = { xeno_captive_is_commander = yes } }
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 1
			effect = {
				add_experience = prev.xeno_spawn_xp
				xeno_copy_captive_xeno_traits = yes
			}
		}
	}
	else_if = {
		limit = { event_target:xeno_dying_captive = { xeno_captive_is_scientist = yes } }
		create_leader = {
			class = scientist
			species = owner_main_species
			name = random
			skill = 1
			effect = {
				add_experience = prev.xeno_spawn_xp
				xeno_copy_captive_xeno_traits = yes
			}
		}
	}
	else = {
		create_leader = {
			class = official
			species = owner_main_species
			name = random
			skill = 1
			effect = {
				add_experience = prev.xeno_spawn_xp
				xeno_copy_captive_xeno_traits = yes
			}
		}
	}

	# The host is consumed by the birth.
	event_target:xeno_dying_captive = {
		kill_leader = { show_notification = no }
	}
}


# ---------------------------------------------------------------------------
# COPY XENO LEADER TRAITS from the dying captive to the newly created leader.
# Runs in NEW LEADER scope (inside create_leader effect). Only copies the mod's
# own Xenomorph leader traits the captive carried, for thematic continuity.
# ---------------------------------------------------------------------------
xeno_copy_captive_xeno_traits = {
	if = {
		limit = { event_target:xeno_dying_captive = { has_trait = leader_trait_xenomorph_royal_bloodline } }
		add_trait = { trait = leader_trait_xenomorph_royal_bloodline }
	}
	if = {
		limit = { event_target:xeno_dying_captive = { has_trait = leader_trait_xenomorph_warrior_caste } }
		add_trait = { trait = leader_trait_xenomorph_warrior_caste }
	}
	if = {
		limit = { event_target:xeno_dying_captive = { has_trait = leader_trait_xenomorph_drone_caste } }
		add_trait = { trait = leader_trait_xenomorph_drone_caste }
	}
	if = {
		limit = { event_target:xeno_dying_captive = { has_trait = leader_trait_xenomorph_worker_breed } }
		add_trait = { trait = leader_trait_xenomorph_worker_breed }
	}
	if = {
		limit = { event_target:xeno_dying_captive = { has_trait = leader_trait_xenomorph_praetorian_lineage } }
		add_trait = { trait = leader_trait_xenomorph_praetorian_lineage }
	}
}


# ---------------------------------------------------------------------------
# ESPIONAGE CONVERSION (country scope). Consumes a Devoted captive, granting
# intel + a spy-network head-start against their origin empire (if known).
# ---------------------------------------------------------------------------
xeno_captive_convert_to_espionage = {
	if = {
		limit = { exists = event_target:xeno_captive_origin_country }
		event_target:xeno_captive_origin_country = {
			# Intel boost on the old empire.
			add_intel = {
				who = root
				amount = 80
			}
		}
		# Establish / boost the spy network value against them.
		if = {
			limit = {
				exists = event_target:xeno_captive_origin_country
				NOT = { root = { is_same_value = event_target:xeno_captive_origin_country } }
			}
			event_target:xeno_captive_origin_country = {
				add_spy_network_level = 25
			}
		}
	}
	else = {
		# Origin unknown (e.g. after save/load): grant a flat intel/influence boon.
		add_resource = { influence = 50 }
	}
	# The captive is consumed in the remaking.
	event_target:xeno_browsed_captive = {
		kill_leader = { show_notification = no }
	}
}


# ---------------------------------------------------------------------------
# RELEASE captive to origin empire (country scope). If the origin is known and
# still exists, the leader returns to them; otherwise the captive is freed
# (removed from your roster).
# ---------------------------------------------------------------------------
xeno_captive_release_to_origin = {
	# Try the in-session event target first (fast path).
	if = {
		limit = {
			exists = event_target:xeno_captive_origin_country
			event_target:xeno_captive_origin_country = { is_country_type = default }
		}
		event_target:xeno_browsed_captive = {
			remove_leader_flag = xeno_captured_leader
			remove_leader_flag = xeno_captive_from_female_origin
			remove_leader_flag = xeno_captive_from_male_origin
			set_owner = event_target:xeno_captive_origin_country
		}
	}
	# Durable path: match the captive's link-id to a country's link-id (survives save/load).
	else_if = {
		limit = { event_target:xeno_browsed_captive = { has_variable = xeno_origin_link } }
		# Find the origin country by matching link-ids.
		random_country = {
			limit = {
				is_country_type = default
				has_variable = xeno_origin_link
				check_variable = {
					which = xeno_origin_link
					value = event_target:xeno_browsed_captive.xeno_origin_link
				}
			}
			save_event_target_as = xeno_relinked_origin
		}
		if = {
			limit = { exists = event_target:xeno_relinked_origin }
			event_target:xeno_browsed_captive = {
				remove_leader_flag = xeno_captured_leader
				remove_leader_flag = xeno_captive_from_female_origin
				remove_leader_flag = xeno_captive_from_male_origin
				set_owner = event_target:xeno_relinked_origin
			}
		}
		else = {
			# Origin no longer exists: free them (they leave your service).
			event_target:xeno_browsed_captive = { kill_leader = { show_notification = no } }
		}
	}
	else = {
		# No origin recorded at all: free them.
		event_target:xeno_browsed_captive = { kill_leader = { show_notification = no } }
	}
}


# ---------------------------------------------------------------------------
# ROSTER CAPACITY (country scope). Recomputes the captive cap into a variable.
#   base 2  (+2 subjugated_flesh tradition)  (+2 tech)  (+2 ascension perk)
# ---------------------------------------------------------------------------
xeno_recompute_captive_capacity = {
	set_variable = { which = xeno_captive_capacity value = 2 }
	# +2: invested in the Subjugated Flesh tradition tree (livestock empire).
	if = {
		limit = { has_tradition = tradition_xenomorph_subjugated_flesh }
		change_variable = { which = xeno_captive_capacity value = 2 }
	}
	# +2: completed the Subjugated Flesh tree (finish bonus).
	if = {
		limit = { has_tradition = tr_xenomorph_subjugated_flesh_finish }
		change_variable = { which = xeno_captive_capacity value = 2 }
	}
	# +2: Hidden Hive Network ascension perk (the espionage/infiltration path).
	if = {
		limit = { has_ascension_perk = ap_xenomorph_hidden_hive_network }
		change_variable = { which = xeno_captive_capacity value = 2 }
	}
}

# Count current captives into a variable (country scope).
xeno_count_captives = {
	set_variable = { which = xeno_captive_count value = 0 }
	every_owned_leader = {
		limit = { has_leader_flag = xeno_captured_leader }
		prev = { change_variable = { which = xeno_captive_count value = 1 } }
	}
}

# Trigger-side helper used at capture time: is there room for another captive?
# (country scope). Recomputes capacity + count, then compares.
xeno_captive_roster_has_room = {
	xeno_recompute_captive_capacity = yes
	xeno_count_captives = yes
}

###############################################
# === xenomorph_capture_effects.txt ===
###############################################
###############################################
# XENOMORPH RAID / CAPTURE SCRIPTED EFFECTS
#
# These effects keep the Hive mod self-contained and mirror the
# Raid-and-Conquer style of saving the attacker, defender, and planet
# into event targets before branching into visible event outcomes.
#
# Primary scopes used by the effects:
#   ROOT/THIS = Xenomorph attacker country when called from a war goal
#   FROM      = defeated/target country when called from a war goal
#   event_target:xeno_raid_attacker = attacker country saved by gate event
#   event_target:xeno_raid_defender = defender country saved by gate event
#   event_target:xeno_raid_planet   = occupied/raided planet saved by gate event
###############################################

# Flag a captured leader and record their origin empire's gender for story branching.
# Call this in LEADER scope; expects event_target:xeno_capture_source_country set to
# the leader's former owner (the defender/target country).
xeno_flag_captured_leader = {
	set_leader_flag = xeno_captured_leader
	xeno_init_captive_state = yes
	# Store the origin empire durably so Release / espionage can target it even
	# across save/load. We mint a random link-id and stamp the SAME id on both the
	# captive leader and the origin country; variables persist across saves, so on
	# Release we match the leader's link-id against every country to find origin.
	if = {
		limit = { exists = event_target:xeno_capture_source_country }
		event_target:xeno_capture_source_country = { save_event_target_as = xeno_captive_origin_country }
		set_variable_to_random_value = {
			which = xeno_origin_link
			min = 1
			max = 2000000000
			rounded = yes
		}
		# Copy the leader's link-id onto the origin country (prev = this leader).
		event_target:xeno_captive_origin_country = {
			set_variable = { which = xeno_origin_link value = prev.xeno_origin_link }
		}
	}
	if = {
		limit = {
			exists = event_target:xeno_capture_source_country
			event_target:xeno_capture_source_country = { xeno_is_matriarchal_hunger_empire = yes }
		}
		set_leader_flag = xeno_captive_from_female_origin
	}
	else_if = {
		limit = {
			exists = event_target:xeno_capture_source_country
			event_target:xeno_capture_source_country = { xeno_is_patriarchal_dominion_empire = yes }
		}
		set_leader_flag = xeno_captive_from_male_origin
	}
}

# Move one non-ruler leader from FROM to ROOT/THIS.
xeno_kidnap_one_leader = {
	from = { save_event_target_as = xeno_kidnap_target_country }
	this = { save_event_target_as = xeno_kidnapper_country }
	# Only capture if the Captive Pits have room (recompute cap + current count).
	xeno_recompute_captive_capacity = yes
	xeno_count_captives = yes
	if = {
		limit = { check_variable = { which = xeno_captive_count value < xeno_captive_capacity } }
		event_target:xeno_kidnap_target_country = {
			save_event_target_as = xeno_capture_source_country
			random_owned_leader = {
				limit = { is_ruler = no }
				save_event_target_as = xeno_captured_leader
				set_owner = event_target:xeno_kidnapper_country
				xeno_flag_captured_leader = yes
			}
		}
	}
}

# Move all eligible leaders from FROM to ROOT/THIS. Rulers are deliberately excluded
# so the victory does not break country leadership state.
xeno_kidnap_all_leaders = {
	from = { save_event_target_as = xeno_kidnap_target_country }
	this = { save_event_target_as = xeno_kidnapper_country }
	event_target:xeno_kidnap_target_country = {
		save_event_target_as = xeno_capture_source_country
		every_owned_leader = {
			limit = { is_ruler = no }
			set_owner = event_target:xeno_kidnapper_country
			xeno_flag_captured_leader = yes
		}
	}
}

# Pick a destination colony owned by the current Xenomorph country.
xeno_save_kidnap_destination = {
	random_owned_planet = {
		limit = {
			is_colony = yes
			free_jobs >= 1
			free_housing > 0
		}
		weights = {
			base = 1
			modifier = {
				factor = 2
				any_owned_pop_group = { is_xenomorph = yes }
			}
		}
		save_event_target_as = xeno_kidnap_destination
	}
}

# Move up to three non-Xenomorph pop groups from a saved planet to a saved destination.
xeno_kidnap_saved_planet_pops = {
	event_target:xeno_raid_attacker = { xeno_save_kidnap_destination = yes }
	event_target:xeno_raid_planet = {
		while = {
			count = 3
			random_owned_pop_group = {
				limit = { is_xenomorph = no }
				save_event_target_as = xeno_kidnapped_pop_group
				resettle_pop = {
					pop = event_target:xeno_kidnapped_pop_group
					planet = event_target:xeno_kidnap_destination
				}
			}
		}
	}
}

# Move one non-ruler leader from the saved defender to the saved attacker.
xeno_kidnap_saved_country_leader = {
	event_target:xeno_raid_defender = {
		save_event_target_as = xeno_capture_source_country
		random_owned_leader = {
			limit = { is_ruler = no }
			save_event_target_as = xeno_captured_leader
			set_owner = event_target:xeno_raid_attacker
			xeno_flag_captured_leader = yes
		}
	}
}

# War-goal compatible pop capture: choose a random colony from FROM and move up to
# three non-Xenomorph pop groups to a viable Xenomorph colony owned by ROOT/THIS.
xeno_kidnap_random_country_pops = {
	from = {
		random_owned_planet = {
			limit = {
				is_colony = yes
				any_owned_pop_group = { is_xenomorph = no }
			}
			save_event_target_as = xeno_raid_planet
		}
	}
	this = {
		save_event_target_as = xeno_raid_attacker
		xeno_save_kidnap_destination = yes
	}
	event_target:xeno_raid_planet = {
		while = {
			count = 3
			random_owned_pop_group = {
				limit = { is_xenomorph = no }
				save_event_target_as = xeno_kidnapped_pop_group
				resettle_pop = {
					pop = event_target:xeno_kidnapped_pop_group
					planet = event_target:xeno_kidnap_destination
				}
			}
		}
	}
}

# Compatibility wrappers used by the Hive mod war goal. These intentionally call
# local effects, not Lustful Void effects, so the war goal works even when LV is absent.
xeno_breeding_offensive_kidnap_pops = {
	xeno_kidnap_random_country_pops = yes
}

xeno_breeding_offensive_kidnap_leaders = {
	xeno_kidnap_all_leaders = yes
}


# ---------------------------------------------------------------------------
# PUT CAPTIVE WITH THE SLAVES (country scope). The "put with slaves" branch of
# the capture choice: the candidate leader is consumed and added to the labor
# pool as a slave pop on a suitable colony. If no colony is suitable, grants a
# small minerals boon representing the conscripted labor.
# Expects event_target:xeno_capture_candidate (the leader) +
# event_target:xeno_capture_source_country (their origin, for species).
# ---------------------------------------------------------------------------
xeno_capture_send_to_slaves = {
	# Try to drop a slave pop on one of our colonies.
	if = {
		limit = {
			any_owned_planet = {
				is_colony = yes
				free_housing > 0
			}
		}
		random_owned_planet = {
			limit = {
				is_colony = yes
				free_housing > 0
			}
			create_pop_group = {
				species = event_target:xeno_capture_source_country
				size = 1
				ethos = random
			}
		}
	}
	else = {
		# No room: the captive is worked to nothing; minor labor boon.
		add_resource = { minerals = 200 }
	}
	# The leader is consumed either way.
	event_target:xeno_capture_candidate = {
		kill_leader = { show_notification = no }
	}
}
